/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.web;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.src.pojos.Cls_Contribuyente;
import com.src.pojos.Cls_Menu;
import com.src.pojos.Cls_Persona;
import com.src.pojos.Cls_PersonaNatural;
import com.src.utiles.Cls_Funciones;
import com.src.utiles.Cls_Json_Data;
import com.src.utiles.Cls_Parametros;
import com.web.utiles.Cls_UtilesParaWeb;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Christian
 */
public class Svl_GestionFacadeWebPersona extends HttpServlet {

    private Gson gson;
    HttpServletRequest request=null;

    public Svl_GestionFacadeWebPersona() {
        gson = new Gson();
    }

    @Override
    public void init(ServletConfig config)throws ServletException{
        super.init(config);
    }
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        this.request=request;
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();        
        int opt=Integer.parseInt(request.getParameter("opt"));
        String resultado="";
        try {
            switch(opt){
                case Cls_Parametros.Persona_OBTENER_TODAS_PERSONAS:
                    resultado=obtenerTodasPersona(request);
                    break;
                case Cls_Parametros.Contribuyente_OBTENER_TODOS_CONTRIBUYENTES:                    
                    resultado=obtenerTodosContribuyentes(request);
                    break;
                case Cls_Parametros.Persona_NUEVA_PERSONA_NATURAL:
                    resultado=insertarPersonaNatural(request);
                    break;
            }
            out.println(resultado);
        } finally { 
            out.close();
        }
    }

    public String obtenerTodasPersona(HttpServletRequest request){
        Map parametros=Cls_UtilesParaWeb.obtenerParametrosDeGrilla(request);
        int pag=0;
        try{
            pag=new Integer(request.getParameter("page")).intValue();
        }catch(NumberFormatException _e){
            pag=1;
        }
//        String filtro=request.getParameter("filtro");
//        parametros.put(Cls_Parametros.Map_FILTRO, filtro);
        ArrayList<Cls_Persona> listPersonas;
        listPersonas=Svl_GestionFacadeWeb.getFacade().obtenerTodasPersonas(parametros);
        return gson.toJson(new Cls_Json_Data(listPersonas,1,new int[] {pag,
        Svl_GestionFacadeWeb.getFacade().obtenerCantidadTodasPersonas(parametros)}));
    }
    public String obtenerTodosContribuyentes(HttpServletRequest request){
        Map parametros=Cls_UtilesParaWeb.obtenerParametrosDeGrilla(request);
        int pag=0;
        try{
            pag=new Integer(request.getParameter("page")).intValue();
        }catch(NumberFormatException _e){
            pag=1;
        }
        String filtro=request.getParameter("filtro");
        parametros.put(Cls_Parametros.Map_FILTRO, filtro);
        ArrayList<Cls_Contribuyente> listContribuyente;
        listContribuyente=Svl_GestionFacadeWeb.getFacade().obtenerTodosContribuyentes(parametros);
        return gson.toJson(new Cls_Json_Data(listContribuyente,1,new int[] {pag,649}));
    }
    
    public String insertarPersonaNatural(HttpServletRequest request){
        Map parametros= new HashMap();
        Cls_PersonaNatural persnat=new Cls_PersonaNatural();
        persnat.setApelpat(request.getParameter("apelpat"));
        persnat.setApelmat(request.getParameter("apelmat"));
        persnat.setNombres(request.getParameter("nombres"));
        persnat.setFechanac(request.getParameter("fechanac"));
        persnat.setSexo(Boolean.parseBoolean(request.getParameter("sexo")));
        parametros.put("obj_personanatural", persnat);
        return gson.toJson(new Cls_Json_Data(Svl_GestionFacadeWeb.getFacade().insertarPersonaNatural(parametros)));        
    }    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
